//
//  AppDelegate+Authority.m
//  OCFrame
//
//  Created by hfzhangzhang on 2017/1/4.
//  Copyright © 2017年 hfzhangzhang. All rights reserved.
//

#import "AppDelegate+Authority.h"
#import "ZZMainTabBarController.h"
#import "LoginViewController.h"

@implementation AppDelegate (Authority)


/** 设置登录页面为rootViewController **/
-(void)showLoginViewController {
    
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: loginVC];
    self.window.rootViewController  = nav;
}

/** 设置主页为rootViewController **/
- (void)showMainTabbarViewController {
    ZZMainTabBarController *mainTabbarVc;
    if ([self.window.rootViewController isKindOfClass:[UITabBarController class]]) {
        mainTabbarVc = (ZZMainTabBarController *)self.window.rootViewController;
    } else {
        mainTabbarVc = [[ZZMainTabBarController alloc] init];
    }
    [mainTabbarVc setSelectedIndex:0];
    [[UIApplication sharedApplication].delegate.window setRootViewController:mainTabbarVc];
    
}

//绑定登录
- (void)loginAuthority {
    [self showMainTabbarViewController];
//    [[KidneyUser sharedInstance] synchronizeFromServerWithSuccess:nil];
//    
//    // 判断登录控制器是否在窗口的根控制器里
//    BOOL(^isLoginViewControllerAtRoot)() = ^{
//        BOOL isRootLoginVC = NO;
//        if (!self.window.rootViewController) {
//            return isRootLoginVC;
//        }
//        isRootLoginVC = ![self.window.rootViewController isKindOfClass:[UITabBarController class]];
//        
//        return isRootLoginVC;
//    };
//    
//    // 跳转到(navigationController)登录->填写资料控制器
//    void(^loadResetInfoViewController)() = ^{
//        if (isLoginViewControllerAtRoot()) {    /// 登录VC在窗口的根控制器里
//            UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
//            if (![[navigationController.viewControllers lastObject] isKindOfClass:[WDFillInfoViewController class]]) {
//                NSMutableArray *viewControllers = [NSMutableArray arrayWithCapacity:2];
//                
//                // 1.登录VC
//                if (navigationController.viewControllers.firstObject) {
//                    [viewControllers addObject:navigationController.viewControllers.firstObject];
//                } else {
//                    [viewControllers addObject:[[LoginViewController alloc] init]];
//                }
    
//                // 2.填写资料VC
//                WDFillInfoViewController *fillInfoViewController = [[WDFillInfoViewController alloc] init];
//                [viewControllers addObject:fillInfoViewController];
//                
//                // 跳转
//                [navigationController setViewControllers:viewControllers animated:navigationController.view.window != nil];
//            }
//            
//        } else {                                /// 登录VC不在窗口的根控制器里
//            UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc] init]];
//            WDFillInfoViewController *fillInfoViewController = [[WDFillInfoViewController alloc] init];
//            // 跳转
//            [navigationController pushViewController:fillInfoViewController animated:NO];
//            self.window.rootViewController = navigationController;
//        }
//    };
//    
//    // 加载当前窗口的根控制器
//    void(^loadRootViewController)() = ^{
//        UserInfoValidateStatus status = -1; // 默认未登陆
//        if ([KidneyUser sharedInstance].isLogin) {
//            status = [KidneyUser sharedInstance].info.attestStatus;
//            
//            if ( [KidneyUser sharedInstance].info != nil) {
//                
//                // MARK: TODO 调试
//                // status = UserInfoValidateStatusUnvalidated;
//                
//                switch (status) {
//                    case UserInfoValidateStatusUnvalidated:
//                        // 跳转到(navigationController)登录->填写信息页面
//                        loadResetInfoViewController();
//                        break;
//                        
//                    case UserInfoValidateStatusValidated:
//                        // 跳转到 (tabBarController)应用主界面
//                        if (self.window.rootViewController == nil || isLoginViewControllerAtRoot()) {
//                            [self showTabbarControllerRepalceRootViewController];
//                        }
//                        //[IUUserDefaultsHelper setObject:@(YES) forKey:kSubmitInfoSuccessNeedsShowKeyName];
//                        // 需要显示审核结果页
//                        if ([[IUUserDefaultsHelper objectForKey:kSubmitInfoSuccessNeedsShowKeyName] boolValue]) {
//                            // 重置标志位
//                            [IUUserDefaultsHelper setObject:@(NO) forKey:kSubmitInfoSuccessNeedsShowKeyName];
//                            WDSubmitInfoViewController *submitInfoViewController = [[WDSubmitInfoViewController alloc] init];
//                            UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:submitInfoViewController];
//                            submitInfoViewController.submitInfo = (WDSubmitInfo)status;
//                            [self.window.rootViewController presentViewController:navigationController animated:NO completion:nil];
//                        }
//                        [self _loginEaseMob];
//                        break;
//                        
//                    case UserInfoValidateStatusReValidating:
//                        // 重置标志位: 下次审核结果需要重新显示审核结果页
//                        //[IUUserDefaultsHelper setObject:@(YES) forKey:kSubmitInfoSuccessNeedsShowKeyName];
//                        // 跳转到(tabBarController)应用主界面
//                        //if (self.window.rootViewController == nil || isLoginViewControllerAtRoot()) {
//                        // [self showTabbarControllerRepalceRootViewController];
//                        //}
//                    case UserInfoValidateStatusValidating:
//                    case UserInfoValidateStatusValidateFailed:
//                        // 重置标志位: 如果显示了 非审核成功的结果页 则 下次审核成功需要重新显示审核成功页
//                        [IUUserDefaultsHelper setObject:@(YES) forKey:kSubmitInfoSuccessNeedsShowKeyName];
//                        // 显示对应审核结果页
//                        // 跳转到(navigationController)登录->审核结果页面
//                        if (isLoginViewControllerAtRoot()) {
//                            UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
//                            
//                            if (![[navigationController.viewControllers lastObject] isKindOfClass:[WDSubmitInfoViewController class]] ||
//                                [(WDSubmitInfoViewController *)[navigationController.viewControllers lastObject] submitInfo] != (WDSubmitInfo)status) {
//                                NSMutableArray *viewControllers = [NSMutableArray arrayWithCapacity:2];
//                                
//                                // 1.登录VC
//                                if (navigationController.viewControllers.firstObject) {
//                                    [viewControllers addObject:navigationController.viewControllers.firstObject];
//                                } else {
//                                    [viewControllers addObject:[[LoginViewController alloc] init]];
//                                }
//                                
//                                // 2.审核进度VC
//                                WDSubmitInfoViewController *submitInfoViewController = [[WDSubmitInfoViewController alloc] init];
//                                submitInfoViewController.submitInfo = (WDSubmitInfo)status;
//                                [viewControllers addObject:submitInfoViewController];
//                                
//                                // 跳转
//                                [navigationController setViewControllers:viewControllers animated:navigationController.view.window != nil];
//                            }
//                        } else {
//                            UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc] init]];
//                            WDSubmitInfoViewController *submitInfoViewController = [[WDSubmitInfoViewController alloc] init];
//                            submitInfoViewController.submitInfo = (WDSubmitInfo)status;
//                            [navigationController pushViewController:submitInfoViewController animated:NO];
//                            self.window.rootViewController = navigationController;
//                        }
//                        [self _loginEaseMob];
//                        break;
//                        
//                    default:    // -1表示没有登录
//                        // 跳转到 (navigationController)登录页面
//                        if (isLoginViewControllerAtRoot()) {
//                            UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
//                            if (!navigationController) {
//                                navigationController = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc] init]];
//                            } else {
//                                [navigationController popToRootViewControllerAnimated:navigationController.view.window != nil];
//                            }
//                            self.window.rootViewController = navigationController;
//                        } else {
//                            [self showLoginViewControllerRepalceRootViewController];
//                        }
//                        [self _logoutEaseMob];
//                        break;
//                }
//            }
//        }else{
//            [self showLoginViewControllerRepalceRootViewController];
//        }
//        
//        //status = 2;
//        
//        
//        [self.window makeKeyAndVisible];
//        //        //集合了启动页、导航页、广告页，这里无需再判断这些是否需要显示，逻辑在里面
//        //        [[WDGlobal global] showLaunch:^(WDSplashView *splashView) {
//        //            if (splashView) {
//        //                splashView = nil;
//        //            }
//        //            //在此调用HomeView的Request方法加载首页数据
//        //
//        //        }];
//    };
//    
//    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:kSubmitInfoResetInfoNotificationName object:nil] subscribeNext:^(id x) {
//        loadResetInfoViewController();
//    }];
//    
//    [RACObserve([KidneyUser sharedInstance], info.attestStatus) subscribeNext:loadRootViewController];
//    [RACObserve([KidneyUser sharedInstance], isLogin) subscribeNext:loadRootViewController];
//    [RACObserve([KidneyUser sharedInstance], imId) subscribeNext:loadRootViewController];
//    
//    /*
//     [RACObserve([KidneyUser sharedInstance], isLogin) subscribeNext:^(id x) {
//     if ([x boolValue] && ![self.window.rootViewController isKindOfClass:[ZZMainTabBarController class]]) {
//     self.window.rootViewController = [[ZZMainTabBarController alloc] init];
//     } else if (![x boolValue] && (self.window.rootViewController == nil || [self.window.rootViewController isKindOfClass:[ZZMainTabBarController class]])) {
//     self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc] init]];
//     }
//     [self _reloadEaseMob];
//     }];
//     */
}



@end
